package com.peihua.controller;

import com.peihua.domain.Notification;
import com.peihua.domain.Result;
import com.peihua.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/notification")
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    // GET /api/notification/latest?type=SYSTEM
    @GetMapping("/latest")
    public Result latest(@RequestParam("type") String type) {
        if (type == null || type.trim().isEmpty()) {
            return Result.error(400L, "type不能为空");
        }
        Notification n = notificationService.latestByType(type.trim().toUpperCase());
        return Result.success(n);
    }

    // GET /api/notification/list?types=SYSTEM,PRIVACY
    @GetMapping("/list")
    public Result list(@RequestParam(value = "types", required = false) String types) {
        List<String> typeList;
        if (types == null || types.trim().isEmpty()) {
            typeList = Arrays.asList("SYSTEM", "PRIVACY");
        } else {
            typeList = Arrays.stream(types.split(","))
                    .filter(s -> s != null && !s.trim().isEmpty())
                    .map(s -> s.trim().toUpperCase())
                    .distinct()
                    .collect(Collectors.toList());
        }
        return Result.success(notificationService.latestByTypes(typeList));
    }
}